#include "Includes.h"

bool bDataCompare(const BYTE* Buffer, const BYTE* Mask1, const char* Mask2)
{
	for(;*Mask2;++Mask2,++Buffer,++Mask1)
	{
		if(*Mask2=='x' && *Buffer!=*Mask1 )
		{
			return false;
		}
	}
	return true;
}

DWORD FindPattern(DWORD Address, DWORD Length, const BYTE* Mask1, const char* Mask2)
{
	for(DWORD i = 0; i < Length; i++)
	{
		if(bDataCompare((BYTE*)(Address+i), Mask1, Mask2))
		{
			return Address+i;
		}
	}
	return 0;
}

bool CheckKeys()
{
	static bool ESPActive = true;
	static bool AimbotActive = false;
	if (GetAsyncKeyState(VK_INSERT) & (1<<15))
	{
		printf("Huhu!\n");
		return true;
	}
	return false;
}

DWORD WINAPI HackThread()
{
	//////////////////////////////////////////////////////////////////////////

	AllocConsole();
	freopen("CONOUT$", "w", stdout);
	freopen("CONOUT$", "w", stderr);
	freopen("CONIN$", "r", stdin);
	SetConsoleTitle(L"DebugOut");
	system("color 06");
	//HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	//_CONSOLE_SCREEN_BUFFER_INFO info;
	//system("mode CON: COLS=200");
	//GetConsoleScreenBufferInfo(ConsoleHandle,&info);
	//SMALL_RECT windowMaxSize = {0, 0, info.dwMaximumWindowSize.X-1, info.dwMaximumWindowSize.Y-1};
	//SetConsoleWindowInfo(ConsoleHandle, TRUE, &windowMaxSize);

	DWORD EntitySize = *(DWORD*)(FindPattern((DWORD)GetModuleHandleA(NULL), 0x06805000 - 0x100, (BYTE*)"\x69\xFF\x00\x00\x00\x00\x81\xC7\x00\x00\x00\x00\x83\x3F\x00\x0F\x84\x00\x00\x00\x00\x8B\x57\x20", "xx????xx????xxxxx????xxx") + 2);

	printf("EntitySize [0x%X] \n\n", EntitySize);

	DWORD EntityArray = *(DWORD*)(FindPattern((DWORD)GetModuleHandleA(NULL), 0x06805000 - 0x100, (BYTE*)"\x81\xC3\x00\x00\x00\x00\xF6\x83\x00\x00\x00\x00\x00\x89\x74\x24\x1C", "xx????xx?????xxxx") + 2);

	printf("EntityArray  [0x%X] \n", EntityArray);

	DWORD RefDef = FindPattern((DWORD)GetModuleHandleA(NULL), 0x06805000 - 0x100, (BYTE*)"\xbe\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x5f\x5e\x5d\x83\xc4\x10", "x????x????xxxxxx");

	printf("RefDef_t [0x%X] \n", RefDef);

	//////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	while (true)
	{
		if (CheckKeys())
		{
			Sleep(250);
		}
		Sleep(10);
	}
	//////////////////////////////////////////////////////////////////////////
	return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
	switch(dwReason)
	{
	case DLL_PROCESS_ATTACH:
		CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)HackThread, NULL, 0, NULL);
		break;
	case DLL_PROCESS_DETACH:
		ExitProcess(0);
		break;
	}
	return TRUE;
}